Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 526)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.37206 11.42829 11.48355 11.53784 11.59115 11.64350 11.69489 11.74531
## [9] 11.79478 11.84330 11.89086 11.93749 11.98316 12.02790 12.07170 12.11458
## [17] 12.15652 12.19753 12.23763 12.27680 12.31504 12.35232 12.38863 12.42398
## [25] 12.45837 12.49178 12.52422 12.55568 12.58617 12.61568 12.64421 12.67175
## [33] 12.69831 12.72388 12.74848 12.77215 12.79486 12.81663 12.83743 12.85728
## [41] 12.87615 12.89405 12.91097 12.92691 12.94185 12.95580 12.96875 12.98069
## [49] 12.99161 13.00152 13.01032 13.01795 13.02444 13.02981 13.03411 13.03737
## [57] 13.03962 13.04088 13.04120 13.04060 13.03912 13.03679 13.03364 13.02971
## [65] 13.02362 13.01421 13.00181 12.98673 12.96930 12.94984 12.92868 12.90613
## [73] 12.88254 12.85821 12.83347 12.80864 12.78406 12.76003 12.73689 12.71497
## [81] 12.69457 12.67603 12.65967 12.64264 12.62225 12.59908 12.57372 12.54675
## [89] 12.51876 12.49031 12.46201 12.43442 12.40813 12.38373 12.36180 12.34292
## [97] 12.32766 12.31455 12.30170 12.28915 12.27692 12.26503 12.25352 12.24241
## [105] 12.23172 12.22149 12.21173 12.20248 12.19375 12.18559 12.17801 12.17103
## [113] 12.16469 12.15943 12.15556 12.15290 12.15130 12.15059 12.15058 12.15113
## [121] 12.15205 12.15319 12.15436 12.15541 12.15616 12.15645 12.15611 12.15542
## [129] 12.15482 12.15436 12.15408 12.15402 12.15423 12.15477 12.15567 12.15699
## [137] 12.15876 12.16104 12.16387 12.16729 12.17137 12.17613 12.18163 12.18791
## [145] 12.19503 12.20302 12.21271 12.22476 12.23892 12.25495 12.27263 12.29170
## [153] 12.31195 12.33311 12.35497 12.37729 12.39982 12.42233 12.44458 12.46635
## [161] 12.48737 12.50744 12.52629 12.54371 12.55944 12.57327 12.58493 12.59664
## [169] 12.61056 12.62643 12.64398 12.66294 12.68305 12.70405 12.72566 12.74762
## [177] 12.76967 12.79154 12.81296 12.83367 12.85340 12.87189 12.88887 12.90407
## [185] 12.91723 12.92808 12.93635 12.94179 12.94412 12.94308 12.93972 12.93527
## [193] 12.92979 12.92330 12.91584 12.90744 12.89814 12.88797 12.87696 12.86516
## [201] 12.85260 12.83931 12.82532 12.81067 12.79283 12.76957 12.74146 12.70904
## [209] 12.67286 12.63348 12.59145 12.54731 12.50162 12.45492 12.40776 12.36070
## [217] 12.31429 12.26908 12.22561 12.18444 12.14612 12.11119 12.08021 12.04906
## [225] 12.01379 11.97520 11.93409 11.89128 11.84755 11.80373 11.76060 11.71899
## [233] 11.67968 11.64348 11.61121 11.58365 11.56162 11.54332 11.52640 11.51081
## [241] 11.49651 11.48347 11.47163 11.46097 11.45143 11.44298 11.43558 11.42919
## [249] 11.42376 11.41926 11.41564 11.41286 11.41088 11.41146 11.41602 11.42403
## [257] 11.43494 11.44821 11.46332 11.47972 11.49686 11.51423 11.53127 11.54745
## [265] 11.56223 11.57507 11.58544 11.59555 11.60785 11.62211 11.63812 11.65567
## [273] 11.67453 11.69450 11.71535 11.73687 11.75885 11.78107 11.80331 11.82536
## [281] 11.84699 11.86801 11.88818 11.90730 11.92515 11.94151 11.95763 11.97481
## [289] 11.99286 12.01159 12.03083 12.05041 12.07013 12.08981 12.10929 12.12837
## [297] 12.14688 12.16464 12.18146 12.19716 12.21208 12.22667 12.24094 12.25490
## [305] 12.26859 12.28201 12.29517 12.30811 12.32083 12.33335 12.34568 12.35785
## [313] 12.36987 12.38176 12.39353 12.40520 12.41679 12.42832 12.43979 12.45124
## [321] 12.46266 12.47451 12.48710 12.50030 12.51399 12.52802 12.54227 12.55659
## [329] 12.57086 12.58495 12.59872 12.61203 12.62475 12.63676 12.64791 12.65807
## [337] 12.66774 12.67748 12.68722 12.69690 12.70648 12.71590 12.72509 12.73402
## [345] 12.74260 12.75081 12.75856 12.76582 12.77252 12.77862 12.78404 12.78858
## [353] 12.79213 12.79480 12.79670 12.79795 12.79866 12.79894 12.79890 12.79865
## [361] 12.79831 12.79798 12.79779 12.79784 12.79824 12.79812 12.79663 12.79393
## [369] 12.79018 12.78557 12.78023 12.77436 12.76809 12.76161 12.75508 12.74866
## [377] 12.74251 12.73680 12.73170 12.72737 12.72397 12.72167 12.72063 12.72102
## [385] 12.72238 12.72412 12.72619 12.72852 12.73109 12.73382 12.73667 12.73959
## [393] 12.74253 12.74544 12.74827 12.75096 12.75347 12.75573 12.75855 12.76259
## [401] 12.76765 12.77354 12.78006 12.78700 12.79416 12.80136 12.80837 12.81502
## [409] 12.82108 12.82637 12.83069 12.83383 12.83560 12.83579 12.83514 12.83452
## [417] 12.83388 12.83322 12.83251 12.83172 12.83084 12.82983 12.82869 12.82737
## [425] 12.82587 12.82415 12.82219 12.81998 12.81718 12.81355 12.80915 12.80407
## [433] 12.79838 12.79216 12.78549 12.77844 12.77110 12.76353 12.75582 12.74805
## [441] 12.74029 12.73262 12.72511 12.71785 12.71091 12.70436 12.69830 12.69279
## [449] 12.68790 12.68226 12.67465 12.66542 12.65490 12.64345 12.63141 12.61912
## [457] 12.60693 12.59517 12.58420 12.57437 12.56600 12.55945 12.55507 12.55225
## [465] 12.55014 12.54869 12.54786 12.54759 12.54785 12.54857 12.54972 12.55124
## [473] 12.55309 12.55522 12.55759 12.56014 12.56283 12.56560 12.56842 12.57123
## [481] 12.57399 12.57664 12.57950 12.58286 12.58667 12.59089 12.59547 12.60036
## [489] 12.60552 12.61090 12.61646 12.62215 12.62792 12.63372 12.63951 12.64524
## [497] 12.65105 12.65710 12.66339 12.66990 12.67663 12.68356 12.69071 12.69805
## [505] 12.70558 12.71329 12.72117 12.72922 12.73743 12.74579 12.75428 12.76287
## [513] 12.77159 12.78043 12.78941 12.79852 12.80779 12.81722 12.82681 12.83658
## [521] 12.84653 12.85667 12.86701 12.87756 12.88832 12.89930
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 526)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.92005 10.99173 11.06225 11.13161 11.19979 11.26678 11.33258 11.39716
## [9] 11.46052 11.52264 11.58351 11.64313 11.70147 11.75852 11.81429 11.86874
## [17] 11.92187 11.97367 12.02413 12.07323 12.12098 12.16743 12.21257 12.25645
## [25] 12.29908 12.34048 12.38067 12.41968 12.45753 12.49424 12.52983 12.56432
## [33] 12.59773 12.63009 12.66114 12.69064 12.71866 12.74524 12.77045 12.79434
## [41] 12.81698 12.83842 12.85871 12.87792 12.89611 12.91333 12.92964 12.94509
## [49] 12.95975 12.97368 12.98585 12.99540 13.00258 13.00766 13.01092 13.01261
## [57] 13.01301 13.01238 13.01099 13.00910 13.00698 13.00490 13.00312 13.00191
## [65] 13.00039 12.99755 12.99346 12.98820 12.98184 12.97447 12.96616 12.95698
## [73] 12.94701 12.93633 12.92501 12.91313 12.90077 12.88799 12.87489 12.86153
## [81] 12.84799 12.83435 12.82068 12.80537 12.78705 12.76619 12.74322 12.71862
## [89] 12.69282 12.66629 12.63948 12.61284 12.58682 12.56188 12.53847 12.51705
## [97] 12.49806 12.47864 12.45594 12.43051 12.40286 12.37352 12.34302 12.31189
## [105] 12.28065 12.24984 12.21998 12.19159 12.16521 12.14137 12.12058 12.10338
## [113] 12.09030 12.07896 12.06682 12.05415 12.04119 12.02820 12.01545 12.00319
## [121] 11.99168 11.98117 11.97192 11.96420 11.95825 11.95434 11.95273 11.95322
## [129] 11.95538 11.95910 11.96427 11.97080 11.97856 11.98745 11.99737 12.00821
## [137] 12.01986 12.03221 12.04515 12.05859 12.07240 12.08649 12.10074 12.11505
## [145] 12.12931 12.14341 12.15963 12.18000 12.20412 12.23156 12.26190 12.29472
## [153] 12.32959 12.36611 12.40384 12.44237 12.48127 12.52013 12.55852 12.59603
## [161] 12.63222 12.66669 12.69901 12.72875 12.75551 12.77885 12.79836 12.81667
## [169] 12.83654 12.85774 12.88005 12.90324 12.92710 12.95138 12.97588 13.00036
## [177] 13.02460 13.04837 13.07144 13.09360 13.11462 13.13427 13.15233 13.16857
## [185] 13.18276 13.19469 13.20413 13.21084 13.21461 13.21522 13.21352 13.21058
## [193] 13.20644 13.20116 13.19477 13.18733 13.17888 13.16948 13.15916 13.14798
## [201] 13.13598 13.12322 13.10973 13.09558 13.07889 13.05805 13.03344 13.00546
## [209] 12.97450 12.94095 12.90521 12.86766 12.82869 12.78871 12.74809 12.70723
## [217] 12.66653 12.62637 12.58715 12.54926 12.51308 12.47902 12.44746 12.41469
## [225] 12.37727 12.33602 12.29176 12.24532 12.19751 12.14915 12.10107 12.05407
## [233] 12.00899 11.96664 11.92785 11.89343 11.86420 11.83690 11.80798 11.77780
## [241] 11.74674 11.71516 11.68343 11.65192 11.62100 11.59104 11.56241 11.53547
## [249] 11.51061 11.48818 11.46855 11.45210 11.43920 11.42955 11.42245 11.41767
## [257] 11.41496 11.41408 11.41478 11.41682 11.41996 11.42395 11.42856 11.43353
## [265] 11.43863 11.44361 11.44822 11.45415 11.46302 11.47458 11.48856 11.50470
## [273] 11.52272 11.54236 11.56335 11.58543 11.60833 11.63178 11.65552 11.67928
## [281] 11.70279 11.72579 11.74801 11.76918 11.78904 11.80731 11.82638 11.84846
## [289] 11.87308 11.89977 11.92805 11.95745 11.98750 12.01771 12.04762 12.07675
## [297] 12.10463 12.13078 12.15473 12.17601 12.19602 12.21647 12.23728 12.25837
## [305] 12.27969 12.30116 12.32270 12.34426 12.36576 12.38712 12.40829 12.42919
## [313] 12.44975 12.46990 12.48957 12.50869 12.52719 12.54500 12.56205 12.57827
## [321] 12.59359 12.60815 12.62216 12.63567 12.64871 12.66135 12.67362 12.68558
## [329] 12.69726 12.70872 12.72000 12.73114 12.74220 12.75322 12.76425 12.77533
## [337] 12.78621 12.79664 12.80664 12.81626 12.82553 12.83449 12.84316 12.85159
## [345] 12.85982 12.86787 12.87578 12.88359 12.89133 12.89903 12.90675 12.91410
## [353] 12.92076 12.92682 12.93236 12.93747 12.94224 12.94676 12.95112 12.95540
## [361] 12.95968 12.96407 12.96864 12.97348 12.97868 12.98404 12.98929 12.99444
## [369] 12.99950 13.00447 13.00935 13.01415 13.01887 13.02352 13.02809 13.03260
## [377] 13.03705 13.04143 13.04576 13.05005 13.05428 13.05847 13.06263 13.06675
## [385] 13.07124 13.07640 13.08209 13.08817 13.09449 13.10090 13.10726 13.11343
## [393] 13.11926 13.12461 13.12933 13.13327 13.13630 13.13827 13.13985 13.14175
## [401] 13.14388 13.14612 13.14840 13.15060 13.15263 13.15439 13.15578 13.15670
## [409] 13.15706 13.15676 13.15570 13.15378 13.15090 13.14696 13.14162 13.13475
## [417] 13.12656 13.11727 13.10712 13.09631 13.08508 13.07363 13.06221 13.05102
## [425] 13.04028 13.03023 13.02107 13.01304 13.00484 12.99515 12.98413 12.97194
## [433] 12.95875 12.94473 12.93003 12.91482 12.89927 12.88354 12.86780 12.85220
## [441] 12.83692 12.82212 12.80795 12.79460 12.78221 12.77096 12.76101 12.75252
## [449] 12.74565 12.73947 12.73297 12.72625 12.71943 12.71259 12.70586 12.69931
## [457] 12.69307 12.68722 12.68188 12.67715 12.67312 12.66991 12.66760 12.66617
## [465] 12.66547 12.66546 12.66607 12.66726 12.66898 12.67117 12.67378 12.67677
## [473] 12.68007 12.68364 12.68743 12.69138 12.69544 12.69956 12.70370 12.70779
## [481] 12.71178 12.71563 12.71964 12.72411 12.72902 12.73434 12.74002 12.74604
## [489] 12.75237 12.75898 12.76582 12.77287 12.78010 12.78747 12.79496 12.80252
## [497] 12.81027 12.81836 12.82676 12.83546 12.84444 12.85371 12.86323 12.87300
## [505] 12.88300 12.89323 12.90366 12.91428 12.92509 12.93606 12.94718 12.95847
## [513] 12.96993 12.98156 12.99338 13.00539 13.01760 13.03002 13.04266 13.05551
## [521] 13.06860 13.08193 13.09550 13.10933 13.12342 13.13777
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 526)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.88490 10.94236 10.99890 11.05449 11.10914 11.16285 11.21561 11.26741
## [9] 11.31826 11.36815 11.41707 11.46503 11.51202 11.55804 11.60308 11.64714
## [17] 11.69021 11.73230 11.77339 11.81349 11.85262 11.89077 11.92797 11.96420
## [25] 11.99948 12.03380 12.06716 12.09957 12.13102 12.16152 12.19108 12.21968
## [33] 12.24734 12.27405 12.29969 12.32416 12.34749 12.36972 12.39087 12.41099
## [41] 12.43010 12.44824 12.46545 12.48175 12.49718 12.51177 12.52556 12.53858
## [49] 12.55087 12.56245 12.57299 12.58216 12.59007 12.59678 12.60240 12.60700
## [57] 12.61067 12.61349 12.61556 12.61696 12.61777 12.61808 12.61797 12.61754
## [65] 12.61569 12.61142 12.60495 12.59652 12.58635 12.57465 12.56167 12.54762
## [73] 12.53273 12.51722 12.50133 12.48527 12.46927 12.45356 12.43836 12.42390
## [81] 12.41040 12.39809 12.38720 12.37601 12.36288 12.34811 12.33203 12.31493
## [89] 12.29714 12.27897 12.26072 12.24270 12.22524 12.20864 12.19321 12.17926
## [97] 12.16712 12.15599 12.14489 12.13382 12.12280 12.11181 12.10087 12.08998
## [105] 12.07914 12.06836 12.05763 12.04697 12.03637 12.02584 12.01538 12.00500
## [113] 11.99470 11.98402 11.97260 11.96062 11.94825 11.93564 11.92297 11.91040
## [121] 11.89811 11.88625 11.87500 11.86453 11.85499 11.84656 11.83940 11.83232
## [129] 11.82414 11.81506 11.80529 11.79502 11.78447 11.77383 11.76330 11.75308
## [137] 11.74339 11.73442 11.72637 11.71944 11.71385 11.70978 11.70745 11.70705
## [145] 11.70878 11.71286 11.71915 11.72729 11.73713 11.74854 11.76137 11.77546
## [153] 11.79067 11.80687 11.82389 11.84160 11.85984 11.87848 11.89736 11.91635
## [161] 11.93528 11.95403 11.97244 11.99036 12.00765 12.02417 12.03976 12.05712
## [169] 12.07875 12.10420 12.13304 12.16484 12.19916 12.23556 12.27361 12.31286
## [177] 12.35289 12.39326 12.43354 12.47327 12.51204 12.54940 12.58492 12.61816
## [185] 12.64868 12.67606 12.69984 12.71960 12.73491 12.74531 12.75297 12.76025
## [193] 12.76706 12.77331 12.77890 12.78373 12.78772 12.79077 12.79278 12.79366
## [201] 12.79332 12.79167 12.78860 12.78402 12.77568 12.76176 12.74282 12.71940
## [209] 12.69207 12.66138 12.62790 12.59218 12.55477 12.51624 12.47714 12.43803
## [217] 12.39946 12.36199 12.32619 12.29260 12.26178 12.23430 12.21070 12.18774
## [225] 12.16211 12.13431 12.10481 12.07412 12.04273 12.01111 11.97977 11.94919
## [233] 11.91987 11.89229 11.86694 11.84432 11.82491 11.80716 11.78929 11.77139
## [241] 11.75361 11.73605 11.71883 11.70208 11.68590 11.67043 11.65578 11.64206
## [249] 11.62940 11.61791 11.60772 11.59894 11.59170 11.58652 11.58367 11.58291
## [257] 11.58396 11.58657 11.59046 11.59538 11.60106 11.60724 11.61366 11.62005
## [265] 11.62616 11.63171 11.63644 11.64159 11.64843 11.65681 11.66657 11.67753
## [273] 11.68953 11.70240 11.71599 11.73013 11.74465 11.75938 11.77417 11.78885
## [281] 11.80325 11.81721 11.83056 11.84314 11.85479 11.86533 11.87562 11.88655
## [289] 11.89804 11.90998 11.92230 11.93490 11.94769 11.96058 11.97350 11.98633
## [297] 11.99900 12.01142 12.02350 12.03514 12.04724 12.06065 12.07521 12.09078
## [305] 12.10721 12.12433 12.14201 12.16008 12.17840 12.19681 12.21517 12.23332
## [313] 12.25110 12.26838 12.28499 12.30079 12.31561 12.32932 12.34176 12.35277
## [321] 12.36221 12.37020 12.37705 12.38288 12.38781 12.39196 12.39546 12.39842
## [329] 12.40097 12.40324 12.40533 12.40738 12.40950 12.41181 12.41445 12.41753
## [337] 12.42042 12.42250 12.42387 12.42464 12.42490 12.42478 12.42437 12.42377
## [345] 12.42310 12.42246 12.42195 12.42169 12.42177 12.42230 12.42338 12.42424
## [353] 12.42412 12.42319 12.42160 12.41950 12.41706 12.41444 12.41179 12.40926
## [361] 12.40702 12.40523 12.40404 12.40361 12.40410 12.40491 12.40539 12.40558
## [369] 12.40555 12.40534 12.40502 12.40463 12.40423 12.40388 12.40363 12.40354
## [377] 12.40365 12.40404 12.40474 12.40582 12.40733 12.40932 12.41185 12.41498
## [385] 12.41944 12.42574 12.43355 12.44259 12.45255 12.46312 12.47399 12.48486
## [393] 12.49543 12.50538 12.51442 12.52224 12.52853 12.53300 12.53681 12.54127
## [401] 12.54626 12.55164 12.55729 12.56309 12.56891 12.57461 12.58008 12.58519
## [409] 12.58981 12.59381 12.59708 12.59947 12.60086 12.60114 12.60025 12.59835
## [417] 12.59557 12.59203 12.58788 12.58325 12.57828 12.57309 12.56783 12.56262
## [425] 12.55761 12.55292 12.54869 12.54506 12.54115 12.53606 12.52993 12.52288
## [433] 12.51502 12.50648 12.49739 12.48785 12.47799 12.46794 12.45782 12.44774
## [441] 12.43783 12.42821 12.41901 12.41033 12.40232 12.39507 12.38873 12.38341
## [449] 12.37923 12.37557 12.37177 12.36789 12.36397 12.36005 12.35619 12.35243
## [457] 12.34882 12.34542 12.34226 12.33940 12.33689 12.33477 12.33310 12.33195
## [465] 12.33137 12.33128 12.33165 12.33241 12.33352 12.33491 12.33654 12.33836
## [473] 12.34030 12.34232 12.34437 12.34638 12.34832 12.35012 12.35173 12.35309
## [481] 12.35417 12.35489 12.35557 12.35653 12.35773 12.35913 12.36070 12.36241
## [489] 12.36421 12.36608 12.36798 12.36988 12.37173 12.37350 12.37517 12.37668
## [497] 12.37817 12.37976 12.38144 12.38320 12.38504 12.38694 12.38889 12.39088
## [505] 12.39290 12.39494 12.39698 12.39903 12.40107 12.40308 12.40506 12.40700
## [513] 12.40891 12.41079 12.41266 12.41451 12.41637 12.41823 12.42010 12.42199
## [521] 12.42391 12.42586 12.42785 12.42989 12.43199 12.43415
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")